#include "bird.h"
#include "ui_bird.h"
#include<QPainter>
#include<QTimer>
#include<QKeyEvent>
bird::bird(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::bird),blsrunbir(false),speed(0)
{
    ui->setupUi(this);
    this->setGeometry(QRect(600,200,890,700));//页面位置及大小
}
bird::~bird()
{
    delete ui;
}

void bird::paintEvent(QPaintEvent *event){
     QPainter painters(this);
     if(!blsrunbir){
         initbir();
     initwall();
     }
     //游戏背景
     painters.setPen(Qt::white);//总
     painters.setBrush(Qt::white);
     painters.drawRect(0,0,10000000,1000000);
     painters.setPen(Qt::blue);//外
     painters.setBrush(Qt::white);
     painters.drawRect(10,40,870,520);
     painters.setPen(Qt::white);//内
     painters.setBrush(Qt::black);
     painters.drawRect(25,55,840,480);
     painters.drawPixmap(25,55,840,480,QPixmap(":/beij/nkxx.png"));
     //分格
     /*painters.setPen(Qt::white);
     for(int i = 25;i <= 265;i+=15){//横
         painters.drawLine(25,i,445,i);
     }
     for(int i = 25;i <= 445;i+=15){//竖
         painters.drawLine(i,25,i,265);
     }*/
     //画墙
     for(int i = 0;i < 100;i++){
     painters.setPen(Qt::blue);
     painters.setBrush(Qt::gray);
     painters.drawRect(rectw[i]);
     painters.drawPixmap(rectw[i],QPixmap(":/beij/ssqs.png"));
     }
     //显示游戏开始与结束
     QFont font1("courier",24);//24号字体
     painters.setFont(font1);
     painters.setPen(Qt::black);
     painters.setBrush(Qt::black);
     painters.drawText(80,670,display);//显示位置
     //显示分数
     QFont font2("courier",15);//15号字体
     painters.setFont(font2);
     painters.setPen(Qt::black);
     painters.setBrush(Qt::black);
     painters.drawText(80,620,"分数：");
     painters.drawText(200,620,QString::number(score/100));//显示位置
     //显示速度
     painters.setPen(Qt::red);
     painters.setBrush(Qt::red);
     if(speed==0){
         painters.drawText(560,620,"前进一 冲！");
     }
     else if(speed==1){
         painters.drawText(560,620,"前进二 冲冲！！");
     }
     else if(speed==2){
         painters.drawText(560,620,"前进三 冲冲冲！！！");
     }
     else if(speed==3){
         painters.drawText(560,620,"前进四 冲冲冲冲！！！");
     }
     //画鸟
     painters.setPen(Qt::white);
     painters.setBrush(Qt::white);
     painters.drawRect(vbirrect);
      painters.drawPixmap(vbirrect,QPixmap(":/beij/nkxh.png"));
     if(blsover){//游戏结束
         display="别灰心,再来一次( ๑ŏ ﹏ ŏ๑ )";
         timer->stop();
         timer1->stop();

     }
     //速度
     if(speed>0){
         painters.drawText(400,3600,"冲冲冲");
     }
}
void bird::initwall(){//所有墙初始化
       rectw.resize(100);
    for(int i = 0;i < 100;i++){
        QRect rectx(830+i*120,(qrand()%530+10),120,30);
        rectw[i]=rectx;
    }
    //一个墙的初始位置(415,25,60,15)
}
void bird::initbir(){
    display="";//开始显示内容
    score=0;
    keyspeed=0;
    blsrunbir=true;
    blsover=false;
    QRect rectb(350,230,30,30);//鸟初始位置及大小
    vbirrect=rectb;
    timer=new QTimer();//设计计时器
    timer->start(100);//每100ms触发一次：鸟的移动速度
    timer1=new QTimer();
    timer1->start(30);//墙和鸟下落速度调节
    connect(timer,SIGNAL(timeout()),SLOT(bir_update()));
    connect(timer1,SIGNAL(timeout()),SLOT(bir_down()));
}
void bird::wall_update(){
    vwallrect.setLeft(vwallrect.left()-10);
    vwallrect.setRight(vwallrect.right()-10);
    update();
}
void bird::bir_update(){
    if(score<30000&&!blsover){
        display="加油，相信你自己(◦˙▽˙◦)";
    }
    if(ndierc_bir==1){//鸟向上蹦
        vbirrect.setTop(vbirrect.top()-20-speed);
        vbirrect.setBottom(vbirrect.bottom()-20-speed);
        ndierc_bir=0;
    }
    if(vbirrect.top()<10){//游戏结束判断上界
        display="撞墙了呜呜，别灰心( ๑ŏ ﹏ ŏ๑ )";
        blsover=true;
    }
    update();//更新
}
void bird::bir_down(){//鸟下落墙移动
    if(keyspeed==1){
        speed++;
        keyspeed=0;
    }
    if(speed>4){//加速
        speed=0;
    }
    vbirrect.setTop(vbirrect.top()+2+speed);
    vbirrect.setBottom(vbirrect.bottom()+2+speed);
    for(int i = 0;i  < 100;i++){
    rectw[i].setLeft(rectw[i].left()-4-speed);
    rectw[i].setRight(rectw[i].right()-4-speed);
    }
    if(vbirrect.bottom()>550){//游戏结束判断出下界
        display="撞墙了呜呜，别灰心( ๑ŏ ﹏ ŏ๑ )";
        blsover=true;
    }
    for(int i = 0;i < 100;i++){//判断是否撞墙
        score++;
        if(rectw[i].left()<=350&&rectw[i].right()>=360){
            if(vbirrect.bottom()>=rectw[i].top()&&vbirrect.bottom()<rectw[i].bottom()){//鸟下界碰撞
                display="撞墙了呜呜，别灰心( ๑ŏ ﹏ ŏ๑ )";
                blsover=true;
            }
            else if(vbirrect.top()<=rectw[i].bottom()&&vbirrect.top()>rectw[i].top()){//鸟上界碰撞
                display="撞墙了呜呜，别灰心( ๑ŏ ﹏ ŏ๑ )";
                blsover=true;
            }
        }
    }
    if(score%100000==0){
        speed=1;
    }
    if(score>30000&&score<100000&&!blsover){
        display="针不戳，玩得针不戳(★^O^★)";
    }
    if(score>100000&&!blsover){
        display="牛蛙，牛袜(•'╻'• )''！！！";
    }
   update();
}
void bird::keyPressEvent(QKeyEvent *event){//键盘事件
    QKeyEvent *key=(QKeyEvent*) event;
    if(key->key()==Qt::Key_Space){
        ndierc_bir=1;
    }
    if(key->key()==Qt::Key_Up){
        keyspeed=1;
    }
}

























